<template>
  <div class="login_container">
    <!-- 栅格布局 -->
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <div class="content">
          <h1>Hello~~Jerry</h1>
          <h2>欢迎来到我的世界</h2>
          <el-form :model="form" :rules="rules" ref="formInstance">
            <el-form-item label="用户名" prop="username">
              <el-input
                v-model="form.username"
                placeholder="请输入用户名"
                :prefix-icon="User"
              />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="form.password"
                placeholder="请输入密码"
                :prefix-icon="Clock"
                type="password"
                show-password
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="onSubmit"
                class="login-btn"
                :loading="loading"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { User, Clock } from '@element-plus/icons-vue'
import { USERSTORE } from '@/store/modules/user.ts'
import { useRouter } from 'vue-router'
import { loginType } from '@/api/user/type'
import type { FormInstance, FormRules } from 'element-plus'

// do not use same name with ref
const form = reactive<loginType>({
  username: 'admin',
  password: 'atguigu123',
})
const router = useRouter()
const loading = ref(false)
const formInstance = ref<FormInstance>()

const onSubmit = () => {
  if (!formInstance.value) return
  formInstance.value.validate(async (valid) => {
    if (valid) {
      // 将登录抽离
      const store = USERSTORE()
      try {
        loading.value = true
        // promise 改变pending状态
        await store.LOGIN(form)
        router.push('/')
      } catch (error) {
        router.push('/login')
      }
      loading.value = false
    } else {
      return false
    }
  })
}

// @ts-expect-error rule 为any
const validateUsername = (rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号需要至少5位'))
  }
}

// @ts-expect-error rule 为any
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码至少6位'))
  }
}

const rules = reactive<FormRules<loginType>>({
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [{ validator: validatePassword, trigger: 'blur' }],
})
</script>
<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url('@/assets/images/background.jpg');
  background-size: cover;

  .content {
    position: relative;
    top: 20vh;
    width: 80%;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    color: #fff;

    h1 {
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      margin: 20px 0;
    }
    .el-form {
      width: 80%;
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
